package com.yw.pms.utils;

import java.util.Map;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class CommonResp<T> {
		/**
		 * @Fields status : 执行状态
		 */
		private int code;
		
		/**
		 * @Fields msg : 消息
		 */
		private String msg;
		
		private Map<String, Object> extraData;
		
		/**
		 * @Fields data : 数据
		 */
		private T data;
		
		public CommonResp() {
		}
		
		public CommonResp(int code, String msg) {
			super();
			this.code = code;
			this.msg = msg;
		}
		
		public CommonResp(int code, String msg, T data) {
			super();
			this.code = code;
			this.msg = msg;
			this.data = data;
		}
		
		public int getCode() {
			return code;
		}
		
		public void setCode(int code) {
			this.code = code;
		}
		
		public String getMsg() {
			return msg;
		}
		
		public void setMsg(String msg) {
			this.msg = msg;
		}
		
		public T getData() {
			return data;
		}
		
		public void setData(T data) {
			this.data = data;
		}
		
		public void success() {
			this.code = 200;
			this.msg = "成功";
		}
		
		public void fail() {
			this.code = 0;
			this.msg = "失败";
		}
		
		public void exception() {
			this.code = -1;
			this.msg = "系统异常";
		}
		
		public boolean isSuccess() {
			return code == 200;
		}
		
		public boolean isFailure() {
			return !isSuccess();
		}
		
		public boolean isException() {
			return code == -1;
		}
		
		public Map<String, Object> getExtraData() {
			return extraData;
		}
		
		public void setExtraData(Map<String, Object> extraData) {
			this.extraData = extraData;
		}
		
		public String toString() {
			try {
				return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
			} catch (Exception e) {
				// 大部分情况下，toString()用在日志输出等调试场景
				return super.toString();
			}
		}
}
